//
//  RecordPlayerView.m
//  2046
//
//  Created by 勇拓 李 on 16/3/22.
//  Copyright © 2016年 2046. All rights reserved.
//

#import "RecordPlayerView.h"
#import "AppDelegate.h"

@interface RecordPlayerView()

@property (nonatomic,strong) NSTimer *timer;//录音声波监控（注意这里暂时不对播放进行监控）
@property (weak, nonatomic) IBOutlet UIButton *btn_sounds;

@end

@implementation RecordPlayerView

- (void)awakeFromNib
{
    [super awakeFromNib];
    self.layer.cornerRadius = 5.f;
    self.layer.masksToBounds = YES;
}

- (void)startRecord
{
    self.frame = CGRectMake((SCREEN_WIDTH - 120)/2, (SCREEN_HEIGHT - 120 - 64)/2, 120, 120);
    [[AppDelegate getMainWindow] addSubview:self];
    
    if (!_timer) {
        _timer = [NSTimer scheduledTimerWithTimeInterval:0.1f target:self selector:@selector(audioPowerChange) userInfo:nil repeats:YES];
    }
    else
    {
        self.timer.fireDate = [NSDate distantPast];
    }
}

- (void)stopRecord
{
    [_timer invalidate];
    _timer = nil;
    [self removeFromSuperview];
}

- (void)dealloc
{
    
}

/**
 *  录音声波状态设置
 */
-(void)audioPowerChange{
    [_audioRecorder updateMeters];//更新测量值
    float power= [_audioRecorder averagePowerForChannel:0];//取得第一个通道的音频，注意音频强度范围时-160到0
    CGFloat progress = -power;
    
    int i = progress/10 - 2;
    
    switch (i) {
        case 0:
            [_btn_sounds setImage:[UIImage imageNamed:@"语音音量1"] forState:UIControlStateNormal];
            break;
        case 1:
            [_btn_sounds setImage:[UIImage imageNamed:@"语音音量2"] forState:UIControlStateNormal];
            break;
        case 2:
            [_btn_sounds setImage:[UIImage imageNamed:@"语音音量3"] forState:UIControlStateNormal];
            break;
        default:
            [_btn_sounds setImage:[UIImage imageNamed:@"语音音量4"] forState:UIControlStateNormal];
            break;
    }
}

@end
